//
//  SearchMsgVC.m
//  ChangChat
//
//  Created by Cocoa on 2019/10/17.
//  Copyright © 2019 yibai. All rights reserved.
//

#import "SearchMsgVC.h"
#import "SearchMsgResultCell.h"
#import <LKDBHelper/LKDBHelper.h>

@interface SearchMsgVC ()
@property (nonatomic, copy) NSString *searchKeyword;
@end

@implementation SearchMsgVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
}

- (void)setupUI {
    self.title = @"查找群聊天记录";
    [self.tableView customRegisterClassWithName:@"SearchMsgResultCell"];
    [self setupSearchVc];
}



#pragma mark - search
- (void)setupSearchVc {
    
    UITextField *searchTF = [[UITextField alloc] initWithFrame:CGRectMake(15, 10, ScreenWidth-20, 35)];
    searchTF.placeholder = @"请输入搜索内容";
    searchTF.font = [UIFont systemFontOfSize:14];
    searchTF.backgroundColor = global_view_LineColor;
    searchTF.layer.cornerRadius = 36/2;
    UIView *leftContentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 13+18+10, 36)];
    UIImageView *imgV = [[UIImageView alloc] initWithImage:UIImage.k_imgName(@"new_search")];
    imgV.frame = CGRectMake(18, 11, 13, 13);
    [leftContentView addSubview:imgV];
    searchTF.leftView = leftContentView;
    searchTF.leftViewMode = UITextFieldViewModeAlways;
    
//    searchTF.delegate = self;
    @weakify(self,searchTF);
    [searchTF.rac_textSignal subscribeNext:^(NSString * _Nullable x) {
        @strongify(self,searchTF);
        if (searchTF.markedTextRange == nil) {
            NSLog(@"xxxx = %@", x);
            self.searchKeyword = x;
            [self fileterMsgList];
        }
    }];
    
    UIView *v = [[UIView alloc] initWithFrame:CGRectZero];
    [v addSubview:searchTF];
    [searchTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.top.mas_offset(10);
        make.right.mas_offset(-15);
        make.bottom.mas_offset(-10);
    }];
    v.backgroundColor = UIColor.whiteColor;
    //    self.view.backgroundColor = self.navigationController.view.backgroundColor;
    [self.view addSubview:v];
    [v mas_makeConstraints:^(MASConstraintMaker *make) {
        if (@available(iOS 11.0, *)) {
            make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop);
        } else {
            make.top.mas_offset(0);
        }
        make.left.right.mas_offset(0);
        make.height.mas_equalTo(56);
    }];
    [self.tableView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.mas_offset(0);
        make.top.equalTo(v.mas_bottom);
    }];
}

- (void)fileterMsgList {
    self.dataSource = nil;
    if (self.searchKeyword.isHaveStr == YES) {
        NSString *whereStr = [NSString stringWithFormat:@"group_id = '%@' and type = 0 and content LIKE '%@%@%@'", self.group.group_id, @"%",self.searchKeyword,@"%"];
        NSMutableArray *msgArr = [MsgItem searchWithWhere:whereStr orderBy:@"createtime asc" offset:0 count:0];
        self.dataSource = msgArr;
    }
    [self.tableView reloadData];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SearchMsgResultCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SearchMsgResultCell" forIndexPath:indexPath];
    cell.keyword = self.searchKeyword;
    cell.msg = self.dataSource[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}


@end
